
package org.sopt.dev.ImgKnight.frontier.Filter;

import java.net.URL;

import org.apache.log4j.Logger;
import org.sopt.dev.ImgKnight.frontier.Frontier;

public class UrlPatternFilter {

	private Frontier frontier = null;
	
	private Logger serviceLogger = null;
	
	public UrlPatternFilter() {
		frontier = Frontier.getInstance();
		serviceLogger = Logger.getLogger("frontier.service");
	}
	
	private boolean isExclusive(String hostName) {
		if( frontier.getFrontierConfig().containsExclusiveUrls(hostName) ) {
			serviceLogger.debug("Exclusive: " + hostName);
			return true;
		}
		return false;
	}
	
	private boolean isAllowd(String url) {
		if( frontier.getFrontierConfig().containsAllowUrlPatterns(url) ) {
			serviceLogger.debug("Allow: " + url);
			return true;
		}
		return false;
	}
	
	private boolean isDeny(String url) {
		if( frontier.getFrontierConfig().containsDenyUrlPatterns(url) ) {
			serviceLogger.debug("Deny: " + url);
			return true;
		}
		return false;
	}
	
	public boolean validate(URL url) {
		String test = url.toString();
		if( isExclusive(url.getHost()) )
			return false;
		if( isAllowd(test) )
			return true;
		if( isDeny(test) )
			return false;
		return (frontier.getFrontierConfig().UrlPatternFilterDefaultAction == 1 ) ? true : false;
	}
	
}
